package com.apache.ius.method;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.ius.IusSqlTools;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义类:portal发起同步IUS参数。
 *
 * @author admin
 */
@SuppressWarnings("serial")
public class InitIusParamsPluginImpl implements CustomMethodPlugin {

	private Logger log = LoggerFactory.getLogger("InitIusParams");

	@Override
	public Object beforeInvoke(Map<String, Object> param) {
		log.info("IUS接口参数-数据同步开始！");
		IDao iusPubDao = (IDao) SpringContextLoader.getBean("iusPubDao");
		long startTime = System.currentTimeMillis();
		String execSql = "select * from sys_param_manager";
		int count = 0;
		if (StrUtil.isNotNull(execSql)) {
			MethodParam params = new MethodParam("ByObjInfo", "", "", IusSqlTools.SPANCENAME);
			params.setParams("dyncSql", execSql);
			List list = iusPubDao.select(params);
			if (!StrUtil.isEmpty(list)) {
				count = list.size();
				for (int i = 0; i < count; i++) {
					Map map = (Map) list.get(i);
					String key = map.get("formName") + "_" + map.get("pageName");
					LoadCacheFactory.getInstance().getCacheManager("iusparamcache").createCacheObject(key, map);
				}
				log.info("IUS接口参数-共计缓存加载(" + count + "条)，执行时间=" + (System.currentTimeMillis() - startTime)+"ms");
			} else {
				log.error("没有获取到ParamManager信息");
			}
		}
		Map<String, String> result = new HashMap<String, String>(2);
		result.put(CustomMethodPlugin.resultMark, "同步成功");
//		result.put(CustomMethodPlugin.pluginInnerReturn, "处理了"+count+"条数据");
		//param.put("businessSuccese", "同步成功");
		log.info("IUS接口参数-数据同步结束："+result);
		return result;
	}

	@Override
	public Object doInvoke(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object afterInvoke(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return null;
	}

}
